package com.scsystem.xysc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.scsystem.xysc.pojo.StoreData;
import com.scsystem.xysc.service.StoreDataService;
import com.scsystem.xysc.mapper.StoreDataMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* @author sky
* @description 针对表【store_data(商铺数据)】的数据库操作Service实现
* @createDate 2023-10-21 12:22:58
*/
@Service
public class StoreDataServiceImpl extends ServiceImpl<StoreDataMapper, StoreData>
    implements StoreDataService{


    @Resource
    private StoreDataMapper storeDataMapper;

    /**
     * 计算店铺指定时间段的成交率
     * @param storeId 店铺ID
     * @param startDate 开始时间
     * @param endDate 结束时间
     * @return Double 成交率
     */
    public Double calculateVisitCompletionRate(Long storeId, LocalDate startDate, LocalDate endDate) {
        // 构建查询条件，查询指定时间段内的数据
        QueryWrapper<StoreData> queryWrapper = new QueryWrapper<StoreData>()
                .eq("store_id", storeId)
                .between("access_date", startDate, endDate);
        List<StoreData> dataInRange = storeDataMapper.selectList(queryWrapper);
        // 计算指定时间段内的成交量和访客数
        int totalDeals = 0;
        for (StoreData data : dataInRange) {
            if ("是".equals(data.getIsDeal())) {
                totalDeals++;
            }
        }
        // 计算指定时间段内的成交率
        return calculateCompletionRate(totalDeals, dataInRange.size());
    }


    /**
     * 计算指定店铺今日的成交率
     * @param storeId 店铺ID
     * @return Double 成交率
     */
    public Double calculateTodayVisitCompletionRate(Long storeId) {
        LocalDate today = LocalDate.now();
        return calculateVisitCompletionRate(storeId, today, today);
    }

    /**
     * 计算指定店铺昨日的成交率
     * @param storeId 店铺ID
     * @return Double 成交率
     */
    public Double calculateYesterdayVisitCompletionRate(Long storeId) {
        LocalDate yesterday = LocalDate.now().minusDays(1);
        return calculateVisitCompletionRate(storeId, yesterday, yesterday);
    }


    /**
     * 计算指定店铺本周的成交率
     * @param storeId 店铺ID
     * @return Double 成交率
     */
    public Double calculateWeeklyVisitCompletionRate(Long storeId) {
        LocalDate today = LocalDate.now();
        LocalDate startOfWeek = today.with(DayOfWeek.MONDAY);
        LocalDate endOfWeek = startOfWeek.plusDays(6);
        return calculateVisitCompletionRate(storeId, startOfWeek, endOfWeek);
    }


    /**
     * 计算指定店铺本月的成交率
     * @param storeId 店铺ID
     * @return Double 成交率
     */
    public Double calculateMonthlyVisitCompletionRate(Long storeId) {
        LocalDate today = LocalDate.now();
        LocalDate firstDayOfMonth = today.withDayOfMonth(1);
        LocalDate lastDayOfMonth = today.withDayOfMonth(today.lengthOfMonth());
        return calculateVisitCompletionRate(storeId, firstDayOfMonth, lastDayOfMonth);
    }


    /**
     * 计算成交率的方法
     * @param totalDeals 成交次数
     * @param totalVisits 访客数量
     * @return Double 成交率
     */
    private Double calculateCompletionRate(int totalDeals, int totalVisits) {
        if (totalVisits == 0) {
            return 0.0;
        }
        return ((double)totalDeals / totalVisits) * 100;
    }


    /**
     * 记录顾客的访问信息，并根据是否成交更新记录
     * @param userIp 顾客的IP地址
     * @param storeId 店铺ID
     * @param isDeal 是否成交
     */
    public void recordCustomerVisit(String userIp, Long storeId, boolean isDeal) {
        LocalDate today = LocalDate.now();

        // 查询数据库中是否有相应的记录
        QueryWrapper<StoreData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("store_id", storeId)
                .eq("user_ip", userIp)
                .eq("access_date", today);
        StoreData existingRecord = storeDataMapper.selectOne(queryWrapper);

        if (existingRecord != null) {
            // 如果记录已存在，更新访问次数和是否成交
            existingRecord.setVisits(existingRecord.getVisits() + 1);
            if (isDeal) {
                existingRecord.setIsDeal("是");
            }
            storeDataMapper.updateById(existingRecord);
        } else {
            // 如果记录不存在，插入新记录
            StoreData newRecord = new StoreData();
            newRecord.setStoreId(storeId);
            newRecord.setUserIp(userIp);
            newRecord.setIsDeal(isDeal ? "是" : "否");
            newRecord.setVisits(1);
            newRecord.setAccessDate(today);
            storeDataMapper.insert(newRecord);
        }
    }
}




